package org.tbjd.client.listeners.projectSwitcher;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.ButtonTitleException;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.exceptions.NoUpperDecisionmakerFoundException;
import org.tbjd.client.exceptions.NoUpperMediatorFoundException;
import org.tbjd.client.interfaces.Decisionmaker;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

public class ProjectRowListener implements ClickListener, Decisionmaker {

	private Widget sender = null;
	private int projectId = 0;
	private int buttonId = 0;

	public void onClick(Widget sender) {
		this.sender = sender;

		try {
			splitTitle(sender);
		} catch (ButtonTitleException e) {
			System.out.println(e);
		}

		switch (buttonId) {
		case NotificationsConstants.STAR_CLICKED:
			System.out.println("ProjectRowListener.onClick() star clicked for project id " + projectId + " !");

			Map<Integer, Integer> updateStarMap = new HashMap<Integer, Integer>();
			updateStarMap.put(NotificationsConstants.TOGGLE_STAR, projectId);
			decide(updateStarMap);
			break;

		case NotificationsConstants.PRIORITY_CLICKED:
			System.out.println("ProjectRowListener.onClick() priority clicked for project id " + projectId + " !");

			Map<Integer, Integer> updatePriorityMap = new HashMap<Integer, Integer>();
			updatePriorityMap.put(NotificationsConstants.TOGGLE_PRIORITY, projectId);
			decide(updatePriorityMap);
			break;

		case NotificationsConstants.DELETE_CLICKED:
			System.out.println("ProjectRowListener.onClick() delete clicked for project id " + projectId + " !");

			Map<Integer, Integer> deleteMap = new HashMap<Integer, Integer>();
			deleteMap.put(NotificationsConstants.DELETE_PROJECT, projectId);
			decide(deleteMap);
			break;

		case NotificationsConstants.INFO_CLICKED:
			System.out.println("ProjectRowListener.onClick() info clicked for project id " + projectId + " !");
			// TODO info clicked; show up an info panel
			break;
		}
	}

	private void splitTitle(Widget sender) throws ButtonTitleException {
		String[] split = sender.getTitle().split(":");
		if (split != null && split.length == 2) {
			projectId = Integer.parseInt(split[0]);
			buttonId = Integer.parseInt(split[1]);
		} else {
			throw new ButtonTitleException();
		}
	}

	public void decide(Object action) {
		try {
			delegate(action);
		} catch (NoUpperDecisionmakerFoundException e) {
			e.toString();
		} catch (NoUpperMediatorFoundException e) {
			e.toString();
		}
	}

	public void delegate(Object action) throws NoUpperDecisionmakerFoundException, NoUpperMediatorFoundException {
		System.out.println("SearchBoxKeyboardListener.delegate(" + sender + ")");

		// passes info to the first responsible
		Widget parent = ((Widget) sender).getParent();
		while (parent != null && parent instanceof Decisionmaker == false) {
			parent = ((Widget) parent).getParent();
		}
		if (parent != null) {
			try {
				((Decisionmaker) parent).decide(action);
			} catch (TBJDException e) {
				e.printStackTrace();
			}
		} else {
			throw new NoUpperDecisionmakerFoundException();
		}
	}

	public void handle(Object action) {
	}
}
